package com.ttc.ssm.controller.admin;

import com.ttc.ssm.controller.common;
import com.ttc.ssm.po.User;
import com.ttc.ssm.service.admin.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by Jiarui on 2016/8/1.
 */
@Controller
@RequestMapping("/admin/setting")
public class SettingController {

    @Autowired
    UserService userService;

    @RequestMapping("/index")
    public String index(Model model){
        return "admin/setting/index";
    }

    @RequestMapping("/change_psw")
    @ResponseBody
    public int change_psw(HttpSession session, String old_psw, String new_psw, String confirm_psw) throws Exception{
        Integer user_id = (Integer) session.getAttribute("admin_id");
        User user = userService.getUserById(user_id);
        if(!new_psw.equals(confirm_psw))
            return -2;
        if(!user.getPassword().equals(common.SHA1(user.getSalt()+common.SHA1(user.getSalt()+common.SHA1(old_psw))))){
            return -1;
        }
        try{
            new_psw = common.SHA1(user.getSalt()+common.SHA1(user.getSalt()+common.SHA1(new_psw)));
            userService.change_psw(user_id,new_psw);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

}
